package com.beidu.lottery.call.controller;

import com.beidu.lottery.call.entity.PortalActivityRule;
import com.beidu.lottery.call.entity.dto.ActivityRegDTO;
import com.beidu.lottery.call.entity.vo.ActivityRegResultVO;
import com.beidu.lottery.call.result.Res;
import com.beidu.lottery.call.service.portal.ActivityRegService;
import com.beidu.lottery.call.service.portal.validator.impl.RegUpdateValidator;
import com.beidu.lottery.call.service.portal.validator.impl.RegValidator;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.constraints.NotNull;

/**
 * <p>
 * 报名活动 前端控制器
 * </p>
 *
 * @author 北渡
 * @since 2023-08-23
 */
@Slf4j
@RestController
@RequestMapping("/activity/reg")
public class ActivityRegController {

    @Resource
    private RegValidator regValidator;

    @Resource
    private ActivityRegService activityRegService;

    @Resource
    private RegUpdateValidator regUpdateValidator;

    /**
     * 报名校验
     *
     * @author 北渡
     */
    @PostMapping("/validate")
    public Res<Boolean> regValidate(@NotNull @RequestParam("id") Integer id) {
        return Res.success(regValidator.validate(id) != null);
    }

    /**
     * 报名数据提交
     *
     * @author 北渡
     */
    @PostMapping("/submit")
    public Res<ActivityRegResultVO> regSubmit(@Validated ActivityRegDTO regDTO) {
        // 报名规则校验
        PortalActivityRule rule = regValidator.validate(regDTO.getActivityId());
        // 报名数据预提交
        return Res.success(activityRegService.regPreSubmit(regDTO, rule));
    }

    /**
     * 报名数据修改
     * <p>
     * TODO 业务变动，暂时弃用
     *
     * @author 北渡
     */
    // @PostMapping("/update")
    @Deprecated
    public Res<Boolean> regUpdate(ActivityRegDTO activityRegDTO) {
        // 修改报名规则校验
        regUpdateValidator.validate(activityRegDTO.getActivityId());
        // 修改报名数据
        return Res.success(activityRegService.regUpdate(activityRegDTO));
    }

    /**
     * 报名结果查询
     *
     * @param id 活动ID
     * @author 北渡
     */
    @GetMapping("/result/{id}")
    public Res<ActivityRegResultVO> queryRegResult(@PathVariable Integer id) {
        return Res.success(activityRegService.queryRegResult(id));
    }

}
